﻿using Core.Touch;
using UnityEngine;
using UnityEngine.UI;

namespace Core
{
    ///<summary>
    /// 屏幕拖动组件
    /// Click与Drag状态互斥
    ///
    /// 关联数据层
    ///
    /// 向上滑屏，浏览下一个游戏
    /// 向下滑屏，浏览上一个游戏（若无上一个，则刷新）
    /// 向左滑屏，进入个人主页
    /// 向右滑屏，功能待定
    /// </summary>
    public class TouchSystem : Component, IAwake, IUpdate
    {
        private GameObject goTouch;
        private GameObject goUIRoot;
        private RenderTexture mRT1;

        private RenderTexture mRT2;

        //private Camera m_ui_camera;
        private Canvas mCanvas;
        private RectTransform rootRect;

        private TouchScreen mTouch;

        public RawImage Img1 { get; private set; }
        public RawImage Img2 { get; private set; }
        public Camera Camera { get; private set; }

        public void Awake()
        {
            goTouch = Utils.Trans.Find(S.gameObject, "Touch");

            var go_cam = Utils.Trans.Find(goTouch, "Camera");
            goUIRoot = Utils.Trans.Find(goTouch, "UIRoot");
            mCanvas = goUIRoot.GetComponent<Canvas>();
            rootRect = goUIRoot.GetComponent<RectTransform>();
            Camera = go_cam.GetComponent<Camera>();

            Img1 = Utils.Trans.Find(goUIRoot, "RawImage1").GetComponent<RawImage>();
            Img2 = Utils.Trans.Find(goUIRoot, "RawImage2").GetComponent<RawImage>();

            var size = S.Setting.Screen.Size;
            var ratio = 0.6f;
            size *= ratio;

            log.err($"Touch.Size={size}");

            mRT1 = new RenderTexture((int)size.Width, (int)size.Height, 16)
            {
                name = "RT1"
            };
            Img1.texture = mRT1;

            mRT2 = new RenderTexture((int)size.Width, (int)size.Height, 16)
            {
                name = "RT2"
            };
            Img2.texture = mRT2;

            var rect = rootRect.rect;
            log.err($"Touch.rect=({rect.width},{rect.height})");

            Img1.rectTransform.sizeDelta = new Vector2(rect.width, rect.height);
            Img2.rectTransform.sizeDelta = new Vector2(rect.width, rect.height);

            Img2.transform.localPosition += Vector3.up * -rect.height;

            S.UI.Camera.targetTexture = mRT1;

            mTouch = new TouchScreen();
            mTouch.Awake(Camera);

            mTouch.OnClick += OnClickHandler;
            mTouch.OnBeginDrag += OnBeginDragHandler;
            mTouch.OnEndDrag += OnEndDragHandler;
            mTouch.OnEndMooring += OnEndMooringHandler;
            mTouch.OnEndReturn += OnEndReturnHandler;
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            mTouch.OnClick -= OnClickHandler;
            mTouch.OnBeginDrag -= OnBeginDragHandler;
            mTouch.OnEndDrag -= OnEndDragHandler;
            mTouch.OnEndMooring -= OnEndMooringHandler;
            mTouch.OnEndReturn -= OnEndReturnHandler;

            mTouch.Dispose();
        }

        public void Update()
        {
            if (Enable == false) return;
            mTouch.Update();
        }

        private bool mEnable;

        public bool Enable
        {
            get => mEnable;
            set
            {
                if (mEnable == value) return;
                mEnable = value;
                //go_touch.SetActive(value);
                goUIRoot.SetActive(value);
                if (mEnable == false)
                {
                    S.UI.Camera.targetTexture = null;
                }
                else
                {
                    S.UI.Camera.targetTexture = mRT1;
                }
            }
        }


        private void OnClickHandler()
        {
            // Debug.Log("OnClickHandler");
            //enable = false;
            //S.ui.Close((int)ESysWnd.social_wnd);
            //S.game.Play();
        }

        private EDir4 mCurrDir;

        private void OnBeginDragHandler(EDir4 dir)
        {
            //Debug.Log($"OnBeginDragHandler dir={dir}");
            if (dir == EDir4.Left || dir == EDir4.Right)
            {
                return;
            }

            if (mCanvas.renderMode != RenderMode.WorldSpace)
            {
                mCanvas.renderMode = RenderMode.WorldSpace;
            }

            mCurrDir = dir;
            if (dir == EDir4.Up)
            {
                Context.SwitchNext();
                S.UI.Camera.targetTexture = mRT2;

                Img2.transform.localPosition += Vector3.up * -rootRect.rect.height;
            }
            else
            {
                Context.SwitchPrev();
                S.UI.Camera.targetTexture = mRT2;

                Img2.transform.localPosition += Vector3.up * rootRect.rect.height;
            }
        }

        private void OnEndDragHandler(EDir4 dir)
        {
            //Debug.Log($"OnEndDragHandler dir={dir}");
        }

        private void OnEndMooringHandler()
        {
            //Debug.Log("OnEndMooringHandler");
            S.UI.Camera.targetTexture = mRT1;
            mTouch.Position = Vector3.zero;
        }

        private void OnEndReturnHandler()
        {
            //Debug.Log("OnEndReturnHandler");
            Context.SwitchPrev();
        }
    }
}
